import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JLabel;
import javax.swing.JPanel;
/**
 * The timeline, tells what state is next.
 * @author Hobosapiens
 */
public class Timeline
{
    private ArrayList<State> stateLine, states;
    private ArrayList<String> stopNames;
    
    //currentStep is the index of the current state
    //currentDist is the number of sailing states that the user has been through
    private int currentStep, currentDist;
    private int totalDist, distToNext, pace, paceScale;
    private State title, shop, sailing, pirate, traveller, victory;
    
    /**
     * Constructor
     * @param ship the ship 
     */
    public Timeline(Ship ship)
    {
    	states = new ArrayList<State>();
    	stopNames = new ArrayList();
    	
    	
    	title = new TitleScreen(ship);
        shop = new Shop(ship);
        sailing = new Sailing(ship);
        pirate = new Pirate(ship); //new
        states.add(title);
        states.add(shop);
        states.add(sailing);
        states.add(pirate); //new
        pace = ship.getPace();
        State victory = new State();
        victory.add(new JLabel("Welcome to the end, champion."));
                
        stateLine = new ArrayList<State>();
        
        //manual stateline addition
        stateLine.add(title);
        stateLine.add(shop);
        stateLine.add(sailing);
        stateLine.add(sailing);
        stateLine.add(shop);
        stateLine.add(sailing);
        stateLine.add(pirate); 
        stateLine.add(sailing);
        stateLine.add(shop);
        stateLine.add(sailing);
        stateLine.add(sailing);
        stateLine.add(sailing);
        stateLine.add(shop);
        stateLine.add(pirate);
        stateLine.add(sailing);
        stateLine.add(pirate);
        stateLine.add(shop);
        stateLine.add(sailing);
        stateLine.add(sailing);

        stateLine.add(victory);

        
        stopNames.add("Louie's boat shop");
        stopNames.add("Louie's boat shop");
        stopNames.add("Lower Atlantic");
        stopNames.add("Mid Atlantic");
        stopNames.add("Jeff's bait and tackle");
        stopNames.add("North Atlantic");
        stopNames.add("VVVVICCCCTTTOOORRRRYYYYYY");
        
        //counts the total number of sailing states since those are the only ones where traveling occurs
        totalDist = 0;
        for (State s: stateLine){
        	if (s.getType() == "Sailing")
        		totalDist++;
        }
        
        currentStep = 0;  //default start at index 0 - title screen
        currentDist = 0;
      
    }
    
    /**
     * Calculates the distance to the next checkpoint 
     */
    public void distanceToNext(){
    	//System.out.println("calculating distance to next");
    	Boolean shopFound = false;
    	int i = currentStep+1;
    	distToNext = 0;
    	
    	while (i < stateLine.size()-1 && !shopFound ){
    		if (stateLine.get(i).getType() == "Sailing"){
    			distToNext++;
    			i++;
    		}
    		else
    			shopFound = true;
    	}
    
    }
    
    /**
     * Calculate the pace scale
     */
    public void calculatePaceScale(){
    	paceScale = pace * 10;
    }
    /**
     * adds the Story object as a Listener to all States used in the game (attribute 'states')
     * Do not manually add as listener 
     * @param story the Story object of the game (unique, only 1). 
     */
    public void addStoryListener(Story story){
    	for (State state:states)
    		state.getStatus().addPropertyChangeListener(story);
    }
    
    /**
     * Getters
     */
    public int getDistToNext(){return distToNext;}
    public int getTotalDist(){return totalDist;}
    public int getCurrentStep(){return currentStep+1;}
    public int getCurrentDist(){return currentDist;}

    public void setDistToNext(int distToNext){this.distToNext=distToNext;}
    public void setTotalDist(int totalDist){this.totalDist=totalDist;}
    public void setCurrentStep(int currentStep){this.currentStep = currentStep;}
    public void setCurrentDist(int currentDist){this.currentDist = currentDist;}
    
    /**
     * Starts the state
     * @return stateLine.get(0) The first state
     */
    public State beginState(){
    	return stateLine.get(0);
    }
    
    /**
     * Returns the current state
     * @return stateLine
     */
    public State currentState()
    {
        if (currentStep >= stateLine.size()) 
    		return null;
        
        return stateLine.get(currentStep);
    }    
    
    /**
     * Returns the current Stop name
     * @return stopNames.get(currentStep) the name of the current stop
     */
    public String currentStopName(){
        if (currentStep >= stopNames.size()) 
    		return null;
        
        return stopNames.get(currentStep);
    }

    /**
     * Returns the upcoming state
     * @return currentState() Returns the current state
     */
    public State nextState()
    {
    	if (currentState().getType() == "Sailing")
    		currentDist++;
        if (currentStep >= stateLine.size()) 
        	return null;
        
        currentStep++;
        calculatePaceScale();
        distanceToNext();
        
        if (currentState().getType()=="Sailing")
        	((Sailing)currentState()).start(5);
                
        return currentState();
    }
    
    /**
     * Adds two sailing states after a pirate cove if user opts to sail around
     * recalculates total distance
     */
    public void sailAround(){
    	int next = getCurrentStep()+1;
    	
    	stateLine.add(next, sailing);
    	stateLine.add(next, sailing);
    	
        totalDist = 0;
        for (State s: stateLine){
        	if (s.getType() == "Sailing")
        		totalDist++;
        }
    }
}